/// <reference path="../declare/dec.d.ts"/>

import getAction, {getActionOfImportPhone} from "../action/action.ts"

let loginAction = getAction('userlogin')
$('#stage').css('height', window.innerHeight);

(() => {
    let username = localStorage.getItem('sgm_remember_username');
    let password = localStorage.getItem('sgm_remember_password');
    if (username && password) {
        $('#username').val(username);
        $('#password').val(password);
        $('#remember-me').prop('checked', true)
    }
})()

/*登录*/
$('#login').on('click', function () {
    let username = $('#username').val();
    let password = $("#password").val();
    loginAction.post({
        username,
        password
    }, {
            success(data) {
                if (data.code == 0) {
                    /*记住密码*/
                    remember(username, password);
                    localStorage.setItem('sgm_chart_session', data.sessionid);
                    /*$.ajax({
                        url: 'http://m.sz404.com/api/getprovince',
                        type: 'post',
                        data: {
                            sessionid: data.sessionid
                        },
                        success(data) {
                            if (data.code == 0) {
                                for (let province of data.provinces) {
                                    $('.select-province').append(`<option value=${province.id}>${province.name}</option>`)
                                }
                            }
                        },
                        error() { }
                    })*/

                    $('input[name="sessionid"]').val(data.sessionid)
                    if (data.import_count !== 0) {
                        $('#already-upload').modal({
                            show: true,
                            backdrop: 'static'
                        })
                    } else {
                        $('#not-upload').modal({
                            show: true,
                            backdrop: 'static'
                        })
                    }
                } else {
                    alert(data.message)
                }
            },
            error() {

            }
        })
})

function remember(username: string, password: string): void {
    localStorage.setItem('sgm_remember_username', username);
    if ($('#remember-me').prop('checked')) {
        localStorage.setItem('sgm_remember_password', password)
    } else {
        localStorage.removeItem('sgm_remember_password');
    }
}

let uploadAction = getActionOfImportPhone('importphone')

$('#upload-go').click(function () {
    var formdata = new FormData(<HTMLFormElement>document.getElementById('upload-form'));
    uploadAction.post(formdata, {
        success(data) {
            if (data.code === 0) {

                location.assign('chart.html')
            } else {
                alert('上传失败')
            }
        },
        error() {
            alert('上传失败')
        }
    })
})

$('#upload-go-2').click(function () {
    var formdata = new FormData(<HTMLFormElement>document.getElementById('upload-form-2'));
    uploadAction.post(formdata, {
        success(data) {
            if (data.code === 0) {
                location.assign('chart.html')
            } else {
                alert('上传失败')
            }
        },
        error() {
            alert('上传失败')
        }
    })
})